package com.itheima.pulltorefreshlib;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.itheima.pulltorefreshlib.view.ObservableScrollView;
import com.itheima.pulltorefreshlib.view.ScrollViewListener;

/**
 * ====================Android App====================<br>
 * 作   者：Hzj<br>
 * 创建时间：2017/12/12 17:29<br>
 * 公   司：浙江有所为网络科技有限公司<br>
 * 修改日志：<br>
 * 作   用：上拉加载下拉刷新，并监听滚动距离的ScrollView<br>
 * ====================Android App====================<br>
 */

public class PullToRefreshObservableScrollView extends PullToRefreshBase<ObservableScrollView> {
    private ObservableScrollView scrollView;

    public PullToRefreshObservableScrollView(Context context) {
        super(context);
    }

    public PullToRefreshObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreshObservableScrollView(Context context, Mode mode) {
        super(context, mode);
    }

    public PullToRefreshObservableScrollView(Context context, Mode mode, AnimationStyle animStyle) {
        super(context, mode, animStyle);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected ObservableScrollView createRefreshableView(Context context, AttributeSet attrs) {
        scrollView = new ObservableScrollView(context, attrs);
        scrollView.setId(R.id.scrollview);
        return scrollView;
    }

    @Override
    protected boolean isReadyForPullStart() {
        return mRefreshableView.getScrollY() == 0;
    }

    @Override
    protected boolean isReadyForPullEnd() {
        View scrollViewChild = mRefreshableView.getChildAt(0);
        return null != scrollViewChild && mRefreshableView.getScrollY() >= (scrollViewChild.getHeight() - getHeight());
    }

    /**
     * 设置滚动监听
     *
     * @param scrollViewListener 滚动监听
     */
    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        if (scrollView != null && scrollViewListener != null)
            scrollView.setScrollViewListener(scrollViewListener);
    }

}
